package com.cp.order.platform.web;

import java.util.HashMap;
import java.util.Map;

import com.cp.order.model.Customer;
import com.cp.order.platform.BaseController;
import com.cp.order.utils.CommonUtils;

public class CustomerController extends BaseController {

	public void index() {
		renderFreeMarker("customer.html");
	}

	public void addCustomer() {
		renderFreeMarker("customerAdd.html");
	}

	public void editCustomer() {
		renderFreeMarker("customerEdit.html");
	}


	public void list() {
		int cpage = getParaToInt("cpage", 1);
		int pageSize = getParaToInt("pagesize", 10);
		HashMap<String, String> params = new HashMap<>();
		returnOK(Customer.dao.queryList(params, cpage, pageSize));
	}

	public void add() {
		Customer customer = getModel(Customer.class, "customer");
		String phoneNum = customer.getCustomerPhone();
		/**
		 * judge 已处理手机号码重复问题，安全起见。再判断一次。
		 */
		Customer customerJudge = Customer.dao.getCustomerByPhoneNum(phoneNum,"");
		if(customerJudge != null) {
			returnBusinessError("手机号码重复，清确认号码是否错误");
		}
		customer.setId(CommonUtils.createUUID());
		customer.setCreateTime(CommonUtils.getDatetime());
		if (customer.save()) {
			returnOK();
		} else {
			returnBusinessError("添加失败");
		}
	}

	public void update() {
		Customer customer = getModel(Customer.class, "customer");
		if (customer.update()) {
			returnOK();
		} else {
			returnBusinessError("修改失败");
		}
	}


	public void delete() {
		String id = getPara("id");
		Customer customer = new Customer();
		customer.setId(id);
		if (customer.delete()) {
			returnOK();
		} else {
			returnBusinessError("删除失败");
		}
	}

	public void judge() {
		Customer customer = Customer.dao.getCustomerByPhoneNum(getPara("customer_phone"),getPara("id"));
		Map<String, Object> map = new HashMap<String, Object>();
		if (customer == null) {
			map.put("isHas", false);
		} else {
			map.put("isHas", true);
		}
		returnOK(map);
	}

}
